package com.hua.designpattern.j2eepattern.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class UserDao implements DAOInterface {

	Map<Integer, User> userRepo = new HashMap<Integer, User>();

	@Override
	public User create(User user) {
		User result = user;
		if (userRepo.get(user.getUserNo()) == null) {
			userRepo.put(user.getUserNo(), user);
		}
		return result;
	}

	@Override
	public Collection<User> retrive() {
		return userRepo.values();
	}

	@Override
	public User retrive(int userNo) {
		return userRepo.get(Integer.valueOf(userNo));
	}

	@Override
	public User update(User user) {
		User result = null;
		if (userRepo.get(user.getUserNo()) != null) {
			userRepo.put(user.getUserNo(), user);
			result = user;
		}
		return result;
	}

	@Override
	public User delete(int userNo) {
		return userRepo.remove(Integer.valueOf(userNo));
	}

}
